import time
import multiprocessing
from multiprocessing import Process
from multiprocessing import Pool

# print('CPU number:' + str(multiprocessing.cpu_count()))


def run1():
    for i in range(2):
        print("lucky is a good man")
        time.sleep(1)


def run2(name, word):
    for i in range(2):
        print("%s is a %s man" % (name, word))
        time.sleep(1)


if __name__ == "__main__":
    t1 = time.time()

    p1 = Process(target=run1)
    p2 = Process(target=run2, args=("lucky", "cool"))

    print('start p1')
    p1.start()
    print('start p2')
    p2.start()

    # 主进程的结束不能影响子进程，所以可以等待子进程的结束再结束主进程
    # 等待子进程结束，才能继续运行主进程
    print('join p1')
    p1.join()       # 阻塞当前进程，直到调用join方法的那个进程执行完，再继续执行当前进程
    print('join p2')
    p2.join()       # 阻塞当前进程，直到调用join方法的那个进程执行完，再继续执行当前进程

    t2 = time.time()
    print("耗时：%.2f" % (t2 - t1))
